package edu.thu.studio.ormdesigner.model.domain;

import java.util.Iterator;

import edu.thu.studio.ormdesigner.model.ObjectSet;

public class DomainSet extends ObjectSet<Domain> {
    
    private static final long serialVersionUID = -6395092030037391244L;
    
    public void merge(Domain newDomain) {
        Iterator<Domain> iter = this.iterator();
        while (iter.hasNext()) {
            Domain oldDomain = iter.next();
            if (null != oldDomain.getId() && null != newDomain.getId()) {
                if (oldDomain.getId().equals(newDomain.getId())) {
                    oldDomain.setCode(newDomain.getCode());
                    oldDomain.setName(newDomain.getName());
                    oldDomain.setComment(newDomain.getComment());
                    oldDomain.setDataType(newDomain.getDataType());
                    oldDomain.setLength(newDomain.getLength());
                    oldDomain.setPrecision(newDomain.getPrecision());
                }
            }
        }
        this.refresh();
    }
    
    public String[] toStringArray() {
        String[] result = new String[this.getObjectList().size()];
        Iterator<Domain> iter = this.iterator();
        int i = 0;
        while (iter.hasNext()) {
            Domain domain = iter.next();
            result[i] = domain.getName();
            i++;
        }
        return result;
    }
    
    public int getIndexById(String id) {
        int index = -1;
        int i = 0;
        Iterator<Domain> iter = this.iterator();
        while (iter.hasNext()) {
            Domain domain = iter.next();
            if (domain.getId().equals(id)) {
                index = i;
                break;
            }
            i++;
        }
        return index;
    }
    
}